<?

require("class.phpmailer.php");

function email_header($title) {
    $get_info = mysql_query("select website_name from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];


    $out_put =
            "
		<html>
		<head>
			<style type='text/css'>
				body{BACKGROUND: #fff; MARGIN: 0px; FONT: 12px/140% 'sans-serif'; TEXT-ALIGN: center}
				td{font-family:tahoma}	
				.text{font:10px}
				.solidborder{font:12px;border:1px solid #000000}
			</style>
		</head>
		
		<body >
		<table id='Table_01' width='600'  border='0' cellpadding='0' cellspacing='0' class='solidborder'>
			<tr>
				<td colspan='2' align='center' height='20'>
					
				</td>
			</tr>";

    if ($title != "") {

        $out_put.="<tr>
				<td colspan='2' bgcolor='#f5a607' align='center' height='20'>
					<font  style='color:#ffffff;font-size:14px;font-weight:bold'>" . strtoupper($title) . "</font>
				</td>
			</tr>";
    }

    $out_put.="
			<tr>
				<td colspan='2' align='left' valign='top'>";

    return $out_put;
}

function email_footer($os_url) {
    $out_put = "	
								
				</TD>
			</tr>
		</table>
		</body>
		</html>
		";

    return $out_put;
}

function email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email) {
    $get_info = mysql_query("select email_host,email_username,email_password,email_fromname,email_reply from tbl_settings where id=1") or die(mysql_error());
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);

    /*
      $mailer = new PHPMailer();
      $mailer = new PHPMailer();
      $mailer->IsSMTP();
      $mailer->isHTML(true);
      $mailer->Host = 'mail.dlookingglass.com:25';
      $mailer->SMTPAuth = TRUE;
      $mailer->Username = 'info@dlookingglass.com';  // Change this to your gmail adress
      $mailer->Password = 'd1gg1d';  // Change this to your gmail password
      $mailer->From = 'info@dlookingglass.com';  // This HAVE TO be your gmail adress
      $mailer->FromName = 'D Looking Glass Admin'; // This is the from name in the email, you can put anything you like here
      $mailer->Body = $body;
      $mailer->Subject = $subject;
     */
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->isHTML(true);
    $mailer->Host = $i["email_host"];
    $mailer->SMTPAuth = TRUE;
    $mailer->Username = $i["email_username"];
    $mailer->Password = $i["email_password"];
    $mailer->From = $i['email_reply'];
    $mailer->FromName = $i['email_fromname'];
    $mailer->Body = $body;
    $mailer->Subject = $subject;



    $emailall = $to;

    $semail = explode(",", $emailall);
    foreach ($semail as $emailadd) {
        $mailer->AddAddress($emailadd);
    }


    if ($bcc != "") {
        $bccall = $bcc;


        $sbcc = explode(",", $bccall);
        foreach ($sbcc as $bccadd) {
            $mailer->AddBCC($bccadd);  // This is where you put the email adress of the person you want to mail
        }
    }


    $attempt = 0;


    while (!$mailer->Send()) {
        if ($attempt == 3) {
            //errRedirect(2, $mailer->ErrorInfo, $_SERVER['PHP_SELF'], 0);
            break;
        }
        $attempt++;
    }
}

function email_new_photo($id) {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];
    $bcc = $i['email_hidden'];


    $to = $i['email_hidden'];
    $subject = FACEBOOK_APP_NAME . "- New Photo Submited";

    $body = email_header("New Photo Submitted");

    $get_class = mysql_query("select a.*,b.member_name,b.fb_id from tbl_photo a inner join tbl_member b on a.fb_id=b.fb_id where a.id='$id'");

    if (mysql_num_rows($get_class) > 0) {
        $a = mysql_fetch_array($get_class, MYSQL_ASSOC);

        $body.="
			<table width='100%'>
                                <tr>
                                    <Td><img src='" . SOURCE_URL_HTTP . "/pictures/contest/" . $a['pic'] . "' width='400'></td>
                                </tr>
                                <tr align='left'>
					<Td>
						Title : " . $a['member_name'] . "
					</td>
				</tr>
				<tr align='left'>
					<Td>
						Title : " . $a['title'] . "
					</td>
				</tr>
				<tr align='left'>
					<Td colspan='2'>Category : " . $a['photo_group'] . "</Td>
				</tr>
				
				<tr align='left'>
					<Td width='30%'>Submitted Date : " . $a['created_datetime'] . "</Td>
				</tr>
				
				
			</table>
		";

        $body.=email_footer($os_url);
        email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email);
    }
}

function email_photo_received($id) {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];
    $bcc = "";


    $to = $i['email_hidden'];
    $subject = "Photo Submission Received";

    $body = email_header("Photo Submission Received");

    $get_class = mysql_query("select * from tbl_photo where id='$id'");

    if (mysql_num_rows($get_class) > 0) {
        $a = mysql_fetch_array($get_class, MYSQL_ASSOC);
        $get_creator = mysql_query("select * from tbl_member where fb_id='" . $a['fb_id'] . "'");
        if (mysql_num_rows($get_creator)) {
            $creator_name = mysql_result($get_creator, 0, "member_name");
        } else {
            $creator_name = "";
        }
        $body.="
			<table width='100%'>
                                <tr>
                                    <Td>Hi $creator_name, <br><br>Thank you for the photo submission. We have received it and it will only be published once we have approved it.</td>
                                </tr>
        
				
				
			</table>
		";

        $body.=email_footer($os_url);
        email_send($body, mysql_result($get_creator, 0, "email"), $bcc, $subject, $websitename_eng, $from_email);
    }
}

function email_new_organization($id) {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];
    $bcc = $i['email_hidden'];


    $to = $i['email_hidden'];
    $subject = $websitename_eng . "- New organization Registered";

    $body = email_header($os_url, $logo_url, $websitename_eng, $i['contact_number'], $i['fax'], "New organization Notification");

    $get_class = mysql_query("select * from tbl_organization where id='$id'");

    if (mysql_num_rows($get_class) > 0) {
        $a = mysql_fetch_array($get_class, MYSQL_ASSOC);
        $get_creator = mysql_query("select * from tbl_user where id='" . $a['uid'] . "'") or die(mysql_error());


        $c = mysql_fetch_array($get_creator, MYSQL_ASSOC);

        $body.="
			<table width='100%'>
				<tr align='left'>
					<Td>
						organization Name : " . $a['organization_name'] . "
					</td>
				</tr>
				<tr align='left'>
					<Td width='30%'>Created By : " . $c['first_name'] . "" . $c['last_name'] . "</Td>
				</tr>
				<tr align='left'>
					<Td>Contact Number : " . $c['contact_number'] . "</Td>
				</tr>
				<tr align='left'>
					<Td><a href='$os_url/org_view_full.php?id=" . $a['id'] . "'>Click here for full details</a></Td>
				</tr>
			</table>
		";

        $body.=email_footer($os_url);
        email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email);
    }
}

function email_password_reset($id) {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];
    $bcc = $i['email_hidden'];



    $subject = $websitename_eng . "- Password Reset";

    $body = email_header($os_url, $logo_url, $websitename_eng, $i['contact_number'], $i['fax'], "Password Reset");

    $get_user = mysql_query("select * from tbl_user where id='$id'");

    if (mysql_num_rows($get_user) > 0) {
        $a = mysql_fetch_array($get_user, MYSQL_ASSOC);

        $to = $a['user_email'];
        $body.="
			<table width='100%'>
                                <Tr>
                                    <td>Hi " . $a['first_name'] . " " . $a['last_name'] . ", we have reset your password.</td>
                                </tr>
				<tr align='left'>
                                    
					<Td>
						Email : " . $a['user_email'] . "
					</td>
				</tr>
				<tr align='left'>
                                    
					<Td>
						New Password : " . $a['user_password'] . "
					</td>
				</tr>
				
				<tr align='left'>
					<Td><a href='$os_url'>Click here to login</a></Td>
				</tr>
			</table>
		";

        $body.=email_footer($os_url);
        email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email);
    }
}

function email_payment_notification($payment_id) {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];
    $bcc = $i['email_hidden'];




    $body = email_header($os_url, $logo_url, $websitename_eng, $i['contact_number'], $i['fax'], "Payment Notification");
    $get_data = mysql_query("select a.id as aid, a.order_id,a.class_id,a.uid,a.amount_paid,a.created_datetime,b.id as bid,b.first_name,b.last_name,b.user_email from tbl_payment a inner join tbl_user b on a.uid=b.id where a.id='$payment_id'");
    $p = mysql_fetch_array($get_data, MYSQL_ASSOC);

    $get_class_info = mysql_query("select a.id as aid,a.uid,a.class_title, b.id as bid,b.first_name,b.last_name, b.user_email from tbl_class a inner join tbl_user b on a.uid=b.id where a.id='" . $p['class_id'] . "'");

    $c = mysql_fetch_array($get_class_info, MYSQL_ASSOC);

    $to = $c['user_email'];
    $subject = $websitename_eng . " - Class Payment Notification";

    $body.="
			<table width='100%'>
				<Tr>
					<Td colspan=2>Dear " . $c['first_name'] . " " . $c['last_name'] . ", a student has registered your class.</td>
				</tr>
				<tr>
					<Td colspan=2><b>Registration Details</b></td>
				</tr>
				<tr>
					<Td width='30%'>Class name</Td><Td>: " . $c['class_title'] . "</td>
				</tr>
				<tr>
					<Td width='30%'>Payer Name</Td><Td>: " . $p['first_name'] . " " . $p['last_name'] . "</td>
				</tr>
				<tr>
					<Td width='30%'>Order ID</Td><Td>: " . $p['order_id'] . "</td>
				</tr>
				<tr>
					<Td width='30%'>Amount Paid</Td><Td>: " . $p['amount_paid'] . "</td>
				</tr>
				<tr>
					<Td width='30%'>Paid Date</Td><Td>: " . $p['created_datetime'] . "</td>
				</tr>
			</table>
		";

    $body.=email_footer($os_url);

    email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email);
}

function email_pre_reg($to, $content, $title) {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];

    $subject = $websitename_eng . " - $title";

    $body = email_header($os_url, $logo_url, $websitename_eng, $i['contact_number'], $i['fax'], "");

    $body.="
			<table width='100%'>
				<tr>
					<Td>
                                        $content
                                        </Td>
				</tr>
			</table>
		";

    $body.=email_footer($os_url);
    $to;
    email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email);
}

function email_test() {

    $get_info = mysql_query("select * from tbl_settings where id=1");
    $i = mysql_fetch_array($get_info, MYSQL_ASSOC);
    $websitename_eng = $i['website_name'];
    $logo_url = $i['logo_url'];
    $os_url = $i['website'];
    $from_email = $i['email'];
    $bcc = $i['email_hidden'];


    $to = $i['email_hidden'];
    $subject = $websitename_eng . "- Test Email";

    $body = email_header($os_url, $logo_url, $websitename_eng, $i['contact_number'], $i['fax'], "Test Email");



    $body.="
			<table width='100%'>
				<tr>
					<Td align='center'>If you can received this email meaning your email is setup properly.</Td>
				</tr>
			</table>
		";

    $body.=email_footer($os_url);
    email_send($body, $to, $bcc, $subject, $websitename_eng, $from_email);
}

?>